<?php
namespace app\api\controller;
use app\admin\model\{UserAddress as AddressModel};
use think\facade\Db;
use hg\apidoc\annotation as Apidoc;


/**
 * @Apidoc\Title("收货地址")
 */

class UserAddress extends ApiBase{


    /**
     * @Apidoc\Title("地址列表")
     * @Apidoc\Url("/api/address_list")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("id", type="int", desc="用户id")
     */
    public function address_list()
    {
        $list   = AddressModel::where('uid',$this->get_user_id())->field('id,real_name,phone,province,city,district,detail,is_default')->select();
        json_response(200,'success',$list??[]);
    }


    /**
     * @Apidoc\Title("添加收货地址")
     * @Apidoc\Url("/api/add_address")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("province", type="string", desc="省份"),
     * @Apidoc\Param("is_default", type="int", desc="是否默认地址0否 1是"),
     * @Apidoc\Param("city", type="string", desc="城市"),
     * @Apidoc\Param("district", type="string", desc="地区"),
     * @Apidoc\Param("detail", type="string", desc="详情"),
     * @Apidoc\Param("real_name", type="string", desc="收货人"),
     * @Apidoc\Param("phone", type="string", desc="收货人手机"),
     * @Apidoc\Returned("id", type="string", desc="收货地址id"),
     */
    public function add(){
        $addressInfo = request()->post();
        if (!isset($addressInfo['province'])) json_response(400,'收货地址格式错误!');
        if (!isset($addressInfo['city'])) json_response(400,'收货地址格式错误!');
        $addressInfo['uid'] = $this->get_user_id();
        //编辑
        if (isset($addressInfo['id']) && AddressModel::where(['id' => $addressInfo['id'], 'uid' => $this->get_user_id()])->count()) {
            $id = $addressInfo['id'];
            unset($addressInfo['id']);
            if (AddressModel::update($addressInfo,['id'=>$id])) {
                if ($addressInfo['is_default'])
                    AddressModel::setDefaultAddress($id, $this->get_user_id());
                json_response(200,'success');
            } else
                json_response(400,'编辑收货地址失败!');
        } else {
            $addressInfo['add_time'] = time();
            if ($address = AddressModel::create($addressInfo)) {
                if ($addressInfo['is_default']) {
                    AddressModel::setDefaultAddress($address->id, $this->get_user_id());
                }
                json_response(200,'success',['id' => $address->id]);
            } else {
               json_response(400,'添加收货地址失败!');
            }
        }
    }

        /**
     * @Apidoc\Title("编辑收货地址")
     * @Apidoc\Url("/api/edit_address")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("province", type="string", desc="省份"),
     * @Apidoc\Param("is_default", type="int", desc="是否默认地址0否 1是"),
     * @Apidoc\Param("city", type="string", desc="城市"),
     * @Apidoc\Param("district", type="string", desc="地区"),
     * @Apidoc\Param("detail", type="string", desc="详情"),
     * @Apidoc\Param("real_name", type="string", desc="收货人"),
     * @Apidoc\Param("phone", type="string", desc="收货人手机"),
     * @Apidoc\Param("id", type="int", desc="收货地址id"),
     */
    public function edit(){
        $addressInfo = request()->post();
        if (!isset($addressInfo['province'])) json_response(400,'收货地址格式错误!');
        if (!isset($addressInfo['city'])) json_response(400,'收货地址格式错误!');
        $addressInfo['uid'] = $this->get_user_id();
        //编辑
        if (isset($addressInfo['id']) && AddressModel::where(['id' => $addressInfo['id'], 'uid' => $this->get_user_id()])->count()) {
            $id = $addressInfo['id'];
            unset($addressInfo['id']);
            if (AddressModel::update($addressInfo,['id'=>$id])) {
                if ($addressInfo['is_default'])
                    AddressModel::setDefaultAddress($id, $this->get_user_id());
                json_response(200,'success');
            } else
                json_response(400,'编辑收货地址失败!');
        } else {
            $addressInfo['add_time'] = time();
            if ($address = AddressModel::create($addressInfo)) {
                if ($addressInfo['is_default']) {
                    AddressModel::setDefaultAddress($address->id, $this->get_user_id());
                }
                json_response(200,'success',['id' => $address->id]);
            } else {
               json_response(400,'添加收货地址失败!');
            }
        }
    }


    /**
     * @Apidoc\Title("获取默认地址")
     * @Apidoc\Url("/api/get_address_default")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("province", type="string", desc="省份"),
     * @Apidoc\Returned("is_default", type="int", desc="是否默认地址0否 1是"),
     * @Apidoc\Returned("city", type="string", desc="城市"),
     * @Apidoc\Returned("district", type="string", desc="地区"),
     * @Apidoc\Returned("detail", type="string", desc="详情"),
     * @Apidoc\Returned("real_name", type="string", desc="收货人"),
     * @Apidoc\Returned("phone", type="string", desc="收货人手机"),
     * @Apidoc\Returned("id", type="string", desc="收货地址id"),
     */
    public function get_address_default()
    {
        $defaultAddress = AddressModel::getUserDefaultAddress($this->get_user_id());
        json_response(1,'success',$defaultAddress??[]);
    }


    /**
     * @Apidoc\Title("设置默认地址")
     * @Apidoc\Url("/api/set_address_default")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("address_id", type="int",require=true, desc="地址id")
     */
    public function set_address_default()
    {
        $id  = param_check('address_id');
        if (!$id || !is_numeric($id)) json_response(0,'参数错误!');
        if (!AddressModel::where(['id' => $id, 'uid' => $this->get_user_id()])->count())
            json_response(0,'地址不存在!');
        $res = AddressModel::setDefaultAddress($id, $this->get_user_id());
        if (!$res)
            json_response(0,'地址不存在!');
        else
            json_response(1,'success');
    }

    /**
     * @Apidoc\Title("删除地址")
     * @Apidoc\Url("/api/del_address")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("address_id", type="int",require=true, desc="地址id")
     */
    public function address_del()
    {
        $id  = param_check('address_id');
        if (!$id || !is_numeric($id)) json_response(400,'参数错误!');
        if (!$address = AddressModel::where(['id' => $id, 'uid' => $this->get_user_id()])->find())
            json_response(400,'地址不存在!');
        if ($address->delete())
            json_response(200,'success');
        else
           json_response(400,'操作失败');
    }

}